<template>
  <div class="donut">
    <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg" class="donut__svg">
        <circle id="donut-graph-x"
                class="donut__svg__scrim"
                r="44" 
                cy="50" 
                cx="50"
                stroke-width="3"
                stroke="#EBEBEB" 
                fill="none" />
        <circle id="donut-graph"
                class="donut__svg__circle--one"
                v-show="!isGot"
                :style="{animationDuration:duration+'s'}"
                @animationend="animationend"
                r="44" 
                cy="50" 
                cx="50"
                stroke-width="3" 
                stroke="#0055A4"
                stroke-linejoin="round"
                stroke-linecap="round" 
                fill="none" />
    </svg>
    <div class="donut__img nogot__img" v-show="!isGot">
        <div class="gotText" :class="{'fadeIn':isClickGet}">
            <img src="http://47.75.99.213/app/cdn/read-got.png">
            <span>已领取</span>
        </div>
        <div class="imgs" @click="getdlb">
            <img src="http://47.75.99.213/app/cdn/read-grey.png" v-show="!isEnd">
            <img src="http://47.75.99.213/app/cdn/read-blue.png" v-show="isEnd" :class="{'fadeOut':isClickGet}">
        </div>
    </div>
    <div class="donut__img"  v-show="isGot">
        <div class="gotText">
            <img src="http://47.75.99.213/app/cdn/read-got.png">
            <span>已领取</span>
        </div>
    </div>
    <div class="donut__text" v-show="!isClickGet && !isGot">
        <span v-if="!isEnd">奖励正在收集中，请稍等</span>
        <span v-else>集满啦！点击即可领取</span>
    </div>
  </div>
</template>
 
<script>
import { Toast } from 'mint-ui';
import { getDlb } from '@/api/api'
import { decryptByAES } from '@/utils/aes'
  export default {
    props: {
        getDlbState: {
            type: String
        },
        duration: {
            type: Number
        }
    },
    data() {
        return {
            isEnd:false,
            isClickGet:false,
            isGot:false
        }
    },
    mounted(){
        if( this.getDlbState == '1' ){
            this.isGot = true
        }else{
            this.isGot = false
        }
    },
    methods:{
        animationend(){
            this.isEnd = true;
        },
        getdlb(){
            if( this.isEnd && !this.isGot){
                //动画倒计时完成 点击领取dlb
                getDlb({
                    "newId": this.$route.query.newId
                }).then(res => {
                    if( res.code == 200 ){
                        this.isGot = true
                        this.isClickGet = true;
                        const data = JSON.parse(decryptByAES(res.data));
                        Toast({
                            message: 'dlb + ' + data.getDlb,
                            iconClass: 'icon iconfont icon-ic_check'
                        });
                    }else{
                        Toast({
                            message: res.resMsg,
                            iconClass: 'icon iconfont icon-dingdanzhuangtaishibai'
                        });
                    }
                })
            }
        }
    }
  }
</script>
 
<style scoped lang="stylus">
.donut{
    position: relative;
    width: 100px;
    height: 100px;
    margin: 20px auto 60px;
    .donut__svg {
        transform: rotate(-90deg);
        .donut__svg__circle--one {
            stroke-dasharray: 276.452;
            stroke-dashoffset: 276;
            animation-name: donut-show-one;
            animation-timing-function:linear;
            animation-fill-mode: forwards;
        }
    }
    .donut__img{
        text-align: center;
        width: 100px;
        height: 100px;
        position: absolute;
        top:0;
        left:0;
        &.nogot__img{
            .gotText{
                opacity: 0;
            }
        }
        .gotText{
            width:100%;
            height:50px;
            text-align: center;
            margin-top: 25px;
            &.fadeIn{
                animation: fadeIn 1s linear 500ms forwards;
            }
            img{
                width:32px;
                height:32px;
                margin:0 auto;
                position: static;
            }
            span{
                font-size:12px;
                color:#999999;
            }
        }
        .imgs{
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            img{
                width:50px;
                height:50px;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-top:-25px;
                margin-left:-25px;
                &.fadeOut{
                    animation: fadeOut 1s linear forwards;
                }
            }
        }
    }
    .donut__text{
        text-align: center;
        width: 140px;
        height: 25px;
        line-height 25px;
        font-size:12px;
        background:#0055A4;
        border-radius 13px;
        color:#fff;
        position: absolute;
        bottom:-35px;
        left:50%;
        margin-left:-70px;
        animation: moveup 2s linear infinite;
        &:after{
            content:'';
            width:0;
            height:0;
            overflow:hidden;
            font-size:0;
            line-height: 0;
            border-width:5px;
            border-style: dashed dashed dashed solid;
            border-color: transparent transparent #0055A4 transparent ;
            position:absolute;
            top:-10px;
            left:65px;
        }
    }
}

@keyframes donut-show-one {
    to {
        stroke-dashoffset: 0;
    }
}

@keyframes moveup {
    0% {
        transform: translate(0px, 0px);
    }
    50% {
        transform: translate(0px, -8px);
    }
    100% {
        transform: translate(0px, 0px);
    }
}
@keyframes fadeOut {
    0% {
        transform: translate(0px, 0px);
        opacity: 1;
    }
    100% {
        transform: translate(0px, -60px);
        opacity: 0;
    }
}
@keyframes fadeIn {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
</style>
